﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using Blog.Models;
using BlogModel;

namespace Blog.Data
{
    public class BlogRepository
    {
        public static BlogDetailsModel GetBlogByUserName(string userName)
        {

            var blog = DB.Context.BlogSet
                .FirstOrDefault(b => b.User.UserName == userName && !b.IsDeleted);

            if (blog == null)
                return null;

            return new BlogDetailsModel
            {
                BlogId = blog.BlogId,
                Name = blog.Name,
                CreationDate = blog.CreationDate,
                Description = blog.Description,
                Topbanner = blog.Topbanner,
                UserId = blog.UserId
            };


        }

        public static int UpdateBlog(BlogDetailsModel blogDetails)
        {
            var blog = new BlogModel.Blog
            {
                BlogId = blogDetails.BlogId,
                Name = blogDetails.Name,
                CreationDate = blogDetails.CreationDate,
                Description = blogDetails.Description,
                Topbanner = blogDetails.Topbanner,
                UserId = blogDetails.UserId
            };

            DB.Context.BlogSet.AddOrUpdate(b=>b.BlogId, blog);

            return DB.Context.SaveChanges();
        }
    }
}